package lyc.springcloud.admin.service.impl;

import lombok.extern.slf4j.Slf4j;
import lyc.springcloud.admin.dao.LoginDao;
import lyc.springcloud.admin.dto.LoginDto;
import lyc.springcloud.admin.service.def.LoginService;
import lyc.springcloud.admin.utils.JwtUtil;
import lyc.springcloud.admin.utils.SM2Util;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class LoginServiceImpl implements LoginService {
    @Autowired
    private LoginDao loginDao;

    /**
     * 登录
     * */
    @Override
    public Map<String, Object> login(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, LoginDto params) {
        String username = params.getUsername();
        String password = params.getPassword();

        // 用户名, 密码不能为空
        if(StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new IllegalArgumentException("用户名, 密码不能为空!");
        }

        // 根据用户名查询用户信息
        Map userInfo = loginDao.selectUserInfoByName(username);
        log.info("根据用户名查询用户信息:{}", userInfo);
        // 根据用户ID查询角色信息
        List<Map> roleInfo = loginDao.selectRoleInfoById((String) userInfo.get("userId"));
        log.info("根据用户ID查询角色信息:{}", roleInfo);
        userInfo.put("roleInfo", roleInfo);

        // 用户名, 密码不匹配 (解密密码为明文匹配)
        String decryptPwd = SM2Util.decrypt(password);
        if(!decryptPwd.equals(userInfo.get("password"))) {
            throw new IllegalArgumentException("用户名, 密码不匹配, 请重新输入!");
        }

        // 当前用户被禁用
        if(userInfo.get("status").equals(0)) {
            throw new IllegalArgumentException("当前用户被禁用, 请联系管理员!");
        }

        // 当前用户已过期
        if(userInfo.get("isExpire").equals(0)) {
            throw new IllegalArgumentException("当前用户已过期, 请联系管理员!");
        }

        // 当前用户被锁定
        if(userInfo.get("isLock").equals(0)) {
            throw new IllegalArgumentException("当前用户被锁定, 请联系管理员!");
        }

        // 当前用户不存在
        if(userInfo.get("isDelete").equals(0)) {
            throw new IllegalArgumentException("当前用户不存在, 请联系管理员!");
        }

        // 生成token并塞到request header
        try {
            String token;
            Map<String, Object> tokenSubject = new HashMap<String, Object>();
            tokenSubject.put("userId", userInfo.get("userId"));
            tokenSubject.put("username", userInfo.get("username"));
            tokenSubject.put("roleInfo", userInfo.get("roleInfo"));
            token = JwtUtil.createToken(tokenSubject);
            log.info("登录生成的token:{}", token);
            httpServletResponse.setHeader("web-token", token);
        } catch (Exception e) {
            log.error("登录生成token异常", e);
        }

        // 生成返回信息
        Map<String, Object> map = new HashMap<>();
        map.put("userInfo", userInfo);
        return map;
    }
}
